﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Ideaa.Data.Models;
using Ideaa.Data.Models;
using Ideaa.Data;
using Ideaa.Services.Interfaces;
using Ideaa.Web.Models;
using Microsoft.AspNet.Identity;
using AutoMapper;

namespace Ideaa.Web.Controllers
{
    public class SuggestionController : Controller
    {
        private ISuggestionService suggestionService;
        private IIdeaService ideaService;

        public SuggestionController(ISuggestionService suggestionService, IIdeaService ideaService)
        {
            this.ideaService = ideaService;
            this.suggestionService = suggestionService;
        }

        // GET: /Suggestion/
        //public ActionResult Index()
        //{
        //    var suggestions = db.Suggestions.Include(s => s.Idea).Include(s => s.User);
        //    return View(suggestions.ToList());
        //}

        // GET: /Suggestion/Details/5
        //public ActionResult Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Suggestion suggestion = db.Suggestions.Find(id);
        //    if (suggestion == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(suggestion);
        //}

        // GET: /Suggestion/Create
        public ActionResult Create(int id)
        {
            Idea idea = this.ideaService.GetIdeaById(id);

            if (idea == null)
            {
                return HttpNotFound();
            }

            SuggestionModel model = new SuggestionModel();
            model.IdeaId = idea.Id;

            return View(model);
        }

        // POST: /Suggestion/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(SuggestionModel model)
        {
            if (ModelState.IsValid)
            {
                Suggestion suggestion = (Suggestion) Mapper.Map(model, model.GetType(), typeof(Suggestion));
                suggestion.UserId = User.Identity.GetUserId();

                //this.suggestionService.Add(suggestion);
                this.ideaService.AddSuggestion(suggestion.UserId, suggestion.Title, suggestion.Content, suggestion.IdeaId);

                return RedirectToAction("Details", "Idea", new { id = model.IdeaId });
            }

            return View(model);
        }

        // GET: /Suggestion/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Suggestion suggestion = db.Suggestions.Find(id);
        //    if (suggestion == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    ViewBag.IdeaId = new SelectList(db.Ideas, "Id", "Title", suggestion.IdeaId);
        //    ViewBag.UserId = new SelectList(db.IdentityUsers, "Id", "UserName", suggestion.UserId);
        //    return View(suggestion);
        //}

        //// POST: /Suggestion/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        //// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include="Id,Title,Content,DateCreatedOnUtc,IdeaId,UserId")] Suggestion suggestion)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(suggestion).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.IdeaId = new SelectList(db.Ideas, "Id", "Title", suggestion.IdeaId);
        //    ViewBag.UserId = new SelectList(db.IdentityUsers, "Id", "UserName", suggestion.UserId);
        //    return View(suggestion);
        //}

        //// GET: /Suggestion/Delete/5
        //public ActionResult Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Suggestion suggestion = db.Suggestions.Find(id);
        //    if (suggestion == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(suggestion);
        //}

        //// POST: /Suggestion/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Suggestion suggestion = db.Suggestions.Find(id);
        //    db.Suggestions.Remove(suggestion);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        db.Dispose();
        //    }
        //    base.Dispose(disposing);
        //}
    }
}
